package routers

import (
	"crypto/tls"
	"strconv"

	"github.com/go-gomail/gomail"
	"shqsoft.cn/pas3ui/base"
)

func SendMail(mailTo []string, subject string, body string) error {
	//定义邮箱服务器连接信息，如果是阿里邮箱 pass填密码，qq邮箱填授权码
	mailConn := map[string]string{
		"user": base.AppConf.Mail.User,
		"pass": base.AppConf.Mail.Pass,
		"host": base.AppConf.Mail.Host,
		"port": base.AppConf.Mail.Port,
	}
	port, _ := strconv.Atoi(mailConn["port"]) //转换端口类型为int
	m := gomail.NewMessage()
	m.SetAddressHeader("From", mailConn["user"], "上海分行绩效考核系统")
	m.SetHeader("To", mailTo...)    //发送给多个用户
	m.SetHeader("Subject", subject) //设置邮箱主题
	m.SetBody("text/html", body)    //设置邮箱正文
	d := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])
	d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
	err := d.DialAndSend(m)
	return err
}
